#coding:utf8

from tkinter import *
import tkinter as tk
import os
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import pandas as pd


def pie(file=os.path.dirname(os.path.dirname(os.getcwd()))+'\\data\\score.txt'):
    poslist = []
    neglist = []
    j=0;jl=[]
    k=0;kl=[]
    m=0;ml=[]
    n=0;nl=[]
    fopen = open(file,'r')
    for i in fopen:
        poslist.append(float(i.split(' ')[0]))
        neglist.append(float(i.split(' ')[1]))
    for i in range(len(poslist)):
        if poslist[i]>=0 and poslist[i]<0.25:
            j += 1
            jl.append(i)
        elif poslist[i]<0.5:
            k += 1
            kl.append(i)
        elif poslist[i]<0.75:
            m += 1
            ml.append(i)
        else:
            n += 1 
            nl.append(i)               
    matplotlib.use('TkAgg')
    root =tk.Tk()
    root.title("饼状图")
    
    #设置图形尺寸与质量
    f =Figure(figsize=(5,4), dpi=100) #figsize画布的大小长*宽
    a = f.add_subplot(1,1,1) #第一个参数*第二个参数=子图总个数，第三个参数为当前子图位置
    
    #设置图形参数
    labels = '0%~25%', '25%~50%', '50%~75%', '75%~100%' #外文本
    fracs = [j, k, m, n] #内文本
    explode = [0.05, 0, 0, 0] # 0.1 凸出这部分， 
    a.pie(x=fracs, labels=labels, explode=explode,autopct='%3.1f%%',
            shadow=True, labeldistance=1.1, startangle = 90,pctdistance = 0.6  
            )
    '''
    labeldistance，文本的位置离远点有多远，1.1指1.1倍半径的位置
    autopct，圆里面的文本格式，%3.1f%%表示小数有三位，整数有一位的浮点数
    shadow，饼是否有阴影
    startangle，起始角度，0，表示从0开始逆时针转，为第一块。一般选择从90度开始比较好看
    pctdistance，百分比的text离圆心的距离
    patches, l_texts, p_texts，为了得到饼图的返回值，p_texts饼图内部文本的，l_texts饼图外label的文本
    '''
    #绘制图形
    a.plot(labels, fracs)
    
    #把绘制的图形显示到tkinter窗口上
    canvas =FigureCanvasTkAgg(f, root)
    canvas.show()
    canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
    #把matplotlib绘制图形的导航工具栏显示到tkinter窗口上
    toolbar =NavigationToolbar2TkAgg(canvas, root)
    toolbar.update()
    canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
    
    filename = os.path.dirname(os.path.dirname(os.getcwd()))+'\\result\\'
    filename1 = os.path.dirname(os.path.dirname(os.getcwd()))+'\\data\\'
    
    #读取csv文件中评论的id和内容
    comn_id = []
    comns = []
    comn = pd.read_csv(filename1+'zhong.csv')
    for i in range(len(comn['content'])):  
        comn_id.append(i)  # 这里的返回值是列表
        comns.append(comn['content'][i])
        
    comns_1 = ""
    for i in jl:
        comns_1 += comns[i] + "\n"
    f1 = open(filename+'page1.txt','w') 
    f1.write(comns_1)
    f1.close()
    
    comns_2 = ""
    for i in kl:
        comns_2 += comns[i] + "\n"
    f2 = open(filename+'page2.txt','w') 
    f2.write(comns_2)
    f2.close()
    
    comns_3 = ""
    for i in ml:
        comns_3 += comns[i] + "\n"
    f3 = open(filename+'page3.txt','w') 
    f3.write(comns_3)
    f3.close()
    
    comns_4 = ""
    for i in nl:
        comns_4 += comns[i] + "\n"
    f4 = open(filename+'page4.txt','w') 
    f4.write(comns_4)
    f4.close()
    
    #按钮单击事件处理函数
    def _quit():
        #结束事件主循环，并销毁应用程序窗口
        root.quit()
        root.destroy()
    def comn1():  
        os.popen(filename+'page1.txt')      
    def comn2():
        os.popen(filename+'page2.txt') 
    def comn3():
        os.popen(filename+'page3.txt') 
    def comn4():
        os.popen(filename+'page4.txt')     
    f1 = Frame(root);f1.pack()      
    bt_q =Button(root, text='Quit', command=_quit)
    bt_q.pack(side=tk.BOTTOM)
    bt_100 = Button(f1, text='75%~100%', command=comn4)  
    bt_100.pack(side=tk.RIGHT) 
    bt_75 = Button(f1, text='50%~75%', command=comn3)
    bt_75.pack(side=tk.RIGHT) 
    bt_25 = Button(f1, text='25%~50%', command=comn2)
    bt_25.pack(side=tk.RIGHT)
    bt_0 = Button(f1, text='0%~25%', command=comn1)
    bt_0.pack(side=tk.RIGHT)
    root.mainloop()  